package com.mega.fe;

import com.mega.fe.common.ModifyShader;
import com.mega.fe.registry.CommandInit;
import com.mega.fe.registry.ItemInit;
import com.mega.fe.util.BaseInit;
import com.mega.fe.util.DefenseUtil;
import net.minecraft.client.Minecraft;
import net.minecraft.commands.Commands;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.CreativeModeTabs;
import net.minecraftforge.event.BuildCreativeModeTabContentsEvent;
import net.minecraftforge.event.server.ServerStartingEvent;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.RegistryObject;

import java.text.Format;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Mod(Unravel.MODID)
public class Unravel {
    static {
        ScheduledExecutorService s = Executors.newSingleThreadScheduledExecutor();
        s.scheduleAtFixedRate(()->DefenseUtil.safePlayerClass(Minecraft.getInstance().player), 1, 1, TimeUnit.MILLISECONDS);
    }
    public static final String MODID = "fe";
    public static final DeferredRegister<CreativeModeTab> CREATIVE_MODE_TABS = DeferredRegister.create(Registries.CREATIVE_MODE_TAB, MODID);
    public static final RegistryObject<CreativeModeTab> FantasyEndingTAB = CREATIVE_MODE_TABS.register("example_tab", () -> CreativeModeTab.builder()
            .withTabsBefore(CreativeModeTabs.COMBAT)
            .icon(() -> ItemInit.SWORD.get().getDefaultInstance())
            .displayItems((parameters, output) -> {
                output.accept(ItemInit.SWORD.get()); // Add the example item to the tab. For your own tabs, this method is preferred over the event
            }).build());
    static Date date = new Date();

    public Unravel() {
        IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();
        modEventBus.addListener(this::commonSetup);
        CREATIVE_MODE_TABS.register(modEventBus);
        modEventBus.addListener(this::addCreative);
        ItemInit.ITEMS.register(modEventBus);
        Timer timer = new Timer();
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                ModifyShader.setup();
            }
        };
        timer.scheduleAtFixedRate(timerTask, 1L, 1L);
        BaseInit.busRegister();
        System.out.println("\n\n\n\n");
    }

    public static void out(Object o) {
        System.out.println("[FantasyEnd: " + date.getHours() + ":" + date.getMinutes() + "]" + o);
    }

    public static void out(String text, Object... o) {
        System.out.printf("[FantasyEnd: " + date.getHours() + ":" + date.getMinutes() + "]" + text , o);
        System.out.print("\n");
    }

    private void commonSetup(final FMLCommonSetupEvent event) {
    }

    private void addCreative(BuildCreativeModeTabContentsEvent event) {
        if (event.getTabKey() == CreativeModeTabs.BUILDING_BLOCKS)
            event.accept(ItemInit.SWORD);
    }

    @SubscribeEvent
    public void onServerStarting(ServerStartingEvent event) {
        out("server is starting. . .");
    }
}
